<template>
  <div class="orderManagement">
    <h3>订单管理</h3>
    <div class="search">
      <div class="cond1 cond">
        <el-input placeholder="请输入订单号" v-model="search1">
          <template slot="prepend">订单号</template>
        </el-input>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入用户姓名" v-model="search2">
          <template slot="prepend">用户姓名</template>
        </el-input>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入用户ID" v-model="search3">
          <template slot="prepend">用户ID</template>
        </el-input>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入邀请人ID" v-model="search11">
          <template slot="prepend">邀请人ID</template>
        </el-input>
      </div>
      <div class="cond2 cond">
        <label>设备</label>
        <el-select v-model="search4" placeholder="请选择">
          <el-option
            v-for="item in optionsDevice"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="cond2 cond">
        <label>状态</label>
        <el-select v-model="search5" placeholder="请选择">
          <el-option
            v-for="item in optionsStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="cond2 cond" v-if="permissions_obj.id_44||permissions_obj.id_all">
        <label>支付方式</label>
        <el-select v-model="search6" placeholder="请选择">
          <el-option
            v-for="item in optionsPayType"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <div class="cond2 cond">
        <label>购买内容</label>
        <el-select v-model="search9" placeholder="请选择">
          <el-option
            v-for="item in optionsBuyContent"
            :key="item.name"
            :label="item.name"
            :value="item.name"
          ></el-option>
        </el-select>
      </div>
      <div class="cond2 cond">
        <label>渠道号</label>
        <el-select v-model="search10" placeholder="请选择">
          <el-option v-for="item in optionsChannel" :key="item" :label="item" :value="item"></el-option>
        </el-select>
      </div>
      <div class="cond2 cond">
        <label>创建时间</label>
        <el-date-picker
          v-model="search7"
          type="datetimerange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>
      </div>
      <div class="cond2 cond">
        <label>支付时间</label>
        <el-date-picker
          v-model="search8"
          type="datetimerange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-search" @click="searchMethod">搜索</el-button>
      </div>
    </div>
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        :row-class-name="tableRowClassName"
      >
        <el-table-column type="index" width="50" header-align="center" align="center" label="序号"></el-table-column>
        <el-table-column header-align="center" align="center" prop="order_sn" label="订单号" sortable></el-table-column>
        <el-table-column header-align="center" align="center" prop="nick" label="用户名" width="100"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="user_id"
          label="用户ID"
          width="80"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="device_text"
          label="设备"
          width="70"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="vip_title"
          label="购买内容"
          width="100"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="user_channel_id"
          label="渠道号"
          width="70"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="user_parent_id"
          label="邀请人id"
          width="75"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="order_amount"
          label="订单金额"
          width="80"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="real_amount"
          label="支付金额"
          width="80"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="status_text"
          label="状态"
          width="70"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="pay_type"
          label="支付方式"
          width="80"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="pay_gate_way"
          label="支付通道"
          width="80"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="pay_time"
          label="支付时间"
          width="100"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="created_at"
          label="创建时间"
          width="100"
        ></el-table-column>
        <el-table-column header-align="center" align="center" label="操作" width="80" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              @click="handleBudan(scope.$index, scope.row)"
              v-if="permissions_obj.id_45||permissions_obj.id_all"
            >补单</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage1"
        :page-sizes="[10,30,50,100]"
        :page-size="pageSize"
        layout="total, prev, pager, next,sizes,jumper"
        :total="totalSize"
      ></el-pagination>
    </div>
    <!-- //补单弹窗 -->
    <el-dialog title="补单" :visible.sync="centerDialogVisible" width="40%" center>
      <div class="goldRow">
        <span>支付单号：</span>
        <el-input placeholder="请输入支付单号" v-model="orderNum" clearable></el-input>
      </div>
      <div class="goldRow">
        <span>备注：</span>
        <textarea v-model="note"></textarea>
      </div>
      <div class="dialogBtn">
        <el-button type="primary" size="small" @click="submitBuDan">提交</el-button>
        <el-button type="info" size="small" @click="cancelBuDan">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "orderManagement",
  data() {
    return {
      optionsDevice: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "I",
          label: "苹果"
        },
        {
          value: "A",
          label: "安卓"
        },
        {
          value: "H",
          label: "来源H5"
        }
      ],
      optionsStatus: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "0",
          label: "待支付"
        },
        {
          value: "1",
          label: "已支付"
        },
        {
          value: "2",
          label: "已取消"
        }
      ],
      optionsPayType: [],
      //购买内容
      optionsBuyContent: [],
      //渠道号
      optionsChannel: [],
      search1: "",
      search2: "",
      search3: "",
      search4: "",
      search5: "",
      search6: "",
      search7: "",
      search8: "",
      search9: "",
      search10: "",
      search11: "",
      tableData: [],
      currentPage1: 1,
      pageSize: 10,
      totalSize: 0,
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      centerDialogVisible: false,
      //订单编号
      orderNum: "",
      //备注
      note: "",
      initData: {},
      selectRow: "",
      // 权限管理
      permissions: [],
      permissions_obj: {},
      flag:true,
    };
  },
  created() {
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff = false;
    this.permissions.forEach((item, index) => {
      if (item == -1) {
        oneOff = true;
      } else {
        this.permissions_obj["id_" + item] = item;
      }
    });
    if (oneOff) {
      this.permissions_obj.id_all = true;
    } else {
      this.permissions_obj.id_all = false;
    }
  },
  mounted() {
    this.getChannelList();
    this.getBuyContentList();
    this.getPayType();
    this.getOderList();
  },
  methods: {
    // 分页
    handleCurrentChange(val) {
      this.currentPage1 = val;
      this.getOderList();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getOderList();
    },
    //显示补单弹窗
    handleBudan(index, row) {
      this.centerDialogVisible = true;
      this.selectRow = row;
      this.orderNum = "";
      this.note = "";
    },
    //提交补单
    async submitBuDan() {
      if(!this.flag){
        return;
      }
      this.flag=false;
      if (!this.orderNum) {
        this.$message({
          message: "请填写支付订单",
          type: "warning"
        });
        return;
      }
      if (!this.note) {
        this.$message({
          message: "请填写备注",
          type: "warning"
        });
        return;
      }
      let parames = {
        handshake: "",
        data: {
          order_sn: this.selectRow.order_sn,
          trade_no: this.orderNum,
          repair_remark: this.note
        }
      };
      let res = await this.$axios.post(
        this.$api.orderAgain,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.flag=true;
        this.centerDialogVisible = false;
      } else {
        this.flag=true;
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //取消补单
    cancelBuDan() {
      this.centerDialogVisible = false;
    },
    //获取支付方式列表
    async getPayType() {
      let parames = {
        handshake: "",
        data: {}
      };
      let res = await this.$axios.post(
        this.$api.orderPayment_data,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        Object.keys(res.data.pay_type).forEach((item, index) => {
          this.optionsPayType.push({
            id: item,
            name: res.data.pay_type[item]
          });
        });
        this.optionsPayType.unshift({
          id: "",
          name: "全部"
        });
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //获取购买内容列表
    async getBuyContentList() {
      let parames = {
        handshake: "",
        data: {}
      };
      let res = await this.$axios.post(
        this.$api.orderViptitle_data,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.optionsBuyContent = res.data;
        this.optionsBuyContent.unshift({
          id: "",
          name: "全部"
        });
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //获取订单列表
    async getOderList() {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage1,
          page_size: this.pageSize,
          search: {
            order_sn: this.search1,
            nick: this.search2,
            user_id: this.search3,
            device_type: this.search4,
            status: this.search5,
            pay_type:this.search6,
            pay_gate_way: "",
            vip_title: this.search9 == "全部" ? "" : this.search9,
            stime:
              this.search7 && this.search7.length > 0 ? this.search7[0] : "",
            otime:
              this.search7 && this.search7.length > 1 ? this.search7[1] : "",
            stime_pay:
              this.search8 && this.search8.length > 0 ? this.search8[0] : "",
            otime_pay:
              this.search8 && this.search8.length > 1 ? this.search8[1] : "",
            user_channel_id: this.search10 == "全部" ? "" : this.search10,
            user_parent_id: this.search11
          }
        }
      };
      let res = await this.$axios.post(
        this.$api.orderList,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        this.initData.data.forEach((item, index) => {
          if (item.device_type == "I") {
            item.device_text = "苹果";
          } else if (item.device_type == "A") {
            item.device_text = "安卓";
          } else if (item.device_type == "H") {
            item.device_text = "来源h5";
          }
          if (item.status == 0) {
            item.status_text = "待支付";
            item.status_booln = false;
          } else if (item.status == 1) {
            item.status_text = "已支付";
            item.status_booln = true;
          } else if (item.status == 2) {
            item.status_text = "已取消";
          }
        });
        this.totalSize = this.initData.total;
        this.tableData = this.initData.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //搜索
    searchMethod() {
      this.currentPage1 = 1;
      this.getOderList();
    },
    //获取渠道号列表
    async getChannelList() {
      let parames = {
        handshake: "",
        data: {}
      };
      let res = await this.$axios.post(
        this.$api.channelCount,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        let channelData = res.data;
        this.optionsChannel = channelData.channel_list;
        this.optionsChannel.unshift("全部");
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.status == 1) {
        return "success-row";
      } else {
        return "";
      }
    }
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
.el-input {
  width: 2.5rem;
}
label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.search .el-select >>> .el-input__inner {
  border-left: none;
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}
.search .el-select {
  width: 1.67rem;
}
.opration {
  margin-top: 0.2rem;
  padding-left: 0.2rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
.dialogBtn {
  display: flex;
  justify-content: center;
  margin-top: 0.3rem;
}
.goldRow {
  margin-bottom: 0.1rem;
}
.goldRow span {
  width: 0.9rem;
  display: inline-block;
}
.goldRow textarea {
  width: 2.5rem;
  height: 1rem;
  padding: 0.05rem;
  border: 1px solid #dcdfe6;
  border-radius: 3px;
}

>>> .el-table .success-row {
  background: #f0f9eb;
}
</style>